package com.hmc.modules.sjfx.controller.sjfx;

import com.hmc.common.utils.Result;
import com.hmc.modules.sjfx.service.lashent.LashentService;
import com.hmc.modules.sjfx.util.FrequencyUtil;
import com.hmc.modules.sjfx.util.ListUtil;
import com.hmc.modules.sjfx.util.NormalDistributionUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.apache.commons.math3.distribution.NormalDistribution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 【上海宝钢】屈强比 [钢管-母材-主材]正态拟合曲线
 */
@AllArgsConstructor
@RestController
@RequestMapping("sjfx/shbgqqb")
@Tag(name = "【上海宝钢】屈强比[钢管-母材-主材]正态拟合曲线")
public class ShBgQqbController {
    @Autowired
    private LashentService lashentService;

    /**
     * 【上海宝钢】屈强比[钢管-母材]正态拟合曲线
     */
    @GetMapping("ggmc")
    @Operation(summary = "【上海宝钢】屈强比[钢管-母材]正态拟合曲线")
    public Result<Map<String, List<Double>>> getGgHf(){
        // 获取钢管母材屈强比
        ArrayList<Double> qqbMc = lashentService.getShBgQqbMc();

        // 排序去重
        List<Double> ggmcKey = ListUtil.sortAndDistinct(qqbMc);

        // 为正态分布曲线生成数据点列表
        List<Double> ggmc = FrequencyUtil.generateProbabilityDistributionCurve(qqbMc);

        ArrayList<Double> ggmcagqqb = new ArrayList<Double>();
        // 计算样本中值的平均数
        double mean = NormalDistributionUtil.getMean(qqbMc);
        // 计算样本中值的标准差
        double sd = NormalDistributionUtil.getStandardDeviation(qqbMc);

        // 创建NormalDistribution对象
        NormalDistribution normalDistribution = new NormalDistribution(mean, sd);
        ggmcKey.forEach(item -> {
            ggmcagqqb.add(normalDistribution.density(item));
        });

        Map<String, List<Double>> map = new HashMap<>();
        map.put("ggmc", ggmc);
        map.put("ggmc", ggmc);
        map.put("ggmcqqb", ggmcagqqb);

        return new Result<Map<String, List<Double>>>().ok(map);
    }

    /**
     * 【上海宝钢】屈强比[主材]正态拟合曲线
     */
    @GetMapping("zcmc")
    @Operation(summary = "【上海宝钢】屈强比[主材]正态拟合曲线")
    public Result<Map<String, List<Double>>> getGgzcHf(){
        // 获取钢管主材屈强比
        ArrayList<Double> qqbZc = lashentService.getShBgQqbZc();

        // 排序去重
        List<Double> zcmcKey = ListUtil.sortAndDistinct(qqbZc);

        // 为正态分布曲线生成数据点列表
        List<Double> zcmc = FrequencyUtil.generateProbabilityDistributionCurve(qqbZc);

        ArrayList<Double> zcmcagqqb = new ArrayList<Double>();
        // 计算样本中值的平均数
        double mean = NormalDistributionUtil.getMean(qqbZc);
        // 计算样本中值的标准差
        double sd = NormalDistributionUtil.getStandardDeviation(qqbZc);

        // 创建NormalDistribution对象
        NormalDistribution normalDistribution = new NormalDistribution(mean, sd);
        zcmcKey.forEach(item -> {
            zcmcagqqb.add(normalDistribution.density(item));
        });

        Map<String, List<Double>> map = new HashMap<>();
        map.put("zcmc", zcmc);
        map.put("zcmc", zcmc);
        map.put("zcmcqqb", zcmcagqqb);

        return new Result<Map<String, List<Double>>>().ok(map);
    }
}
